package cc.sjfx.sharedoc.dialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bean.FileInfo;
import cc.sjfx.sharedoc.utils.CheckUtils;
import cc.sjfx.sharedoc.widget.adapter.FileSelectAdapter;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class FileManageDialog extends Dialog {
	private List<String> items = null;
	private List<String> paths = null;
	private String rootPath = "/sdcard/";
	private String curPath = "/";
	private ListView listView;
	private TextView mPath;
	Context context;

	public FileManageDialog(Context context) {
		super(context, R.style.MyDialog);
		this.context = context;
		setContentView(R.layout.fileselect);
		setCanceledOnTouchOutside(false);
		mPath = (TextView) findViewById(R.id.mPath);
		listView = (ListView) findViewById(R.id.fileList);
		getFileDir(rootPath);
	}

	public void setFileListener(final DialogFileListener listener) {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				File file = new File(paths.get(arg2));
				if (file.isDirectory()) {
					curPath = paths.get(arg2);
					getFileDir(paths.get(arg2));
				} else {
					saveFile(file, listener);
				}
			}
		});
	}

	private void getFileDir(String filePath) {
		mPath.setText(filePath);
		items = new ArrayList<String>();
		paths = new ArrayList<String>();
		File f = new File(filePath);
		File[] files = f.listFiles();
		if (!filePath.equals(rootPath)) {
			items.add("b1");
			paths.add(rootPath);
			items.add("b2");
			paths.add(f.getParent());
		}
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			String type=getMIMEType(file);
			if(!file.isDirectory()&&CheckUtils.stringIsNull(type)){
				continue;
			}
			items.add(file.getName());
			paths.add(file.getPath());
		}
		listView.setAdapter(new FileSelectAdapter(context, items, paths));
	}

	private void saveFile(File file, DialogFileListener listener) {
		// TODO Auto-generated method stub
		// Intent data = new Intent(MyFileManager.this,
		// ReleaseExamActivity.class);
		if (file.length() > 10240000) {
			listener.onError(1);
		} else if (!CheckUtils.stringIsNull(getMIMEType(file))) {
			FileInfo info = new FileInfo();
			info.setFilePath(file.getPath());
			info.setFileName(file.getName());
			info.setFileType(getMIMEType(file));
			info.setFileSize(file.length());
			listener.onSuccess(info);
			this.cancel();
		}else {
			listener.onError(2);
		}
	}
	private String getMIMEType(File f) {
		String type = "";
		String fName = f.getName();
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();
		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			type = "image";
		} else if (end.equals("doc") ||end.equals("txt") || end.equals("docs") || end.equals("ppt") || end.equals("pptx") || end.equals("excel")) {
			type = "docment";
		}else if (end.equals("rar") || end.equals("zip") || end.equals("7z")) {
			type = "zip";
		}else if (end.equals("pdf") ) {
			type="pdf";
		}
		return type;
	}

	public interface DialogFileListener {
		public void onSuccess(FileInfo fileInfo);

		public void onError(int status);
	}

}
